package com.luboyan.CBillbill.controller;

import com.luboyan.CBillbill.service.file.FileService;
import com.luboyan.CBillbill.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

/**
 * 文件上传控制类
 */
@RestController
@RequestMapping("files")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 获得文件唯一标识
     */
    @PostMapping("/encrypt")
    public Result<String> getFileMd5(MultipartFile file) throws Exception {
        String fileMd5 = fileService.getFileMd5(file);
        return new Result<>(fileMd5);
    }

    /**
     * 分片上传文件
     */
    @PutMapping("/upload")
    public Result<String> uploadFileByShard(MultipartFile file,
                                   String fileMd5,
                                   Integer shard,
                                   Integer shardTotal) throws Exception{
        HashMap<String, String> filePath = fileService.uploadFileByShard(file, fileMd5, shard, shardTotal);
        if(filePath.containsKey("秒传")){
            return new Result<>("205", filePath.get("秒传"));
        }
        return new Result<>(filePath.get("普通"));
    }

    /**
     * 上传视频封面
     */
    @PutMapping("/img")
    public Result<String> uploadImg(MultipartFile file,
                                    String fileMd5) throws Exception{
        String imgPath = fileService.uploadImg(file, fileMd5);
        return new Result<>(imgPath);
    }
}
